package com.javaweb.aop;

import com.javaweb.model.User;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @desc:
 * @author: Ping
 * @date: 2024/9/23 15:49
 */
@Component
@Aspect
@Slf4j
public class PerformanceLogAspect {

    /**
     * define pointcuts
     */

    /**
     * 用户模块中的所有查询： select*
     */
    @Pointcut("within(com.javaweb.dao..*) && execution(public * select*(..))")
    public void selectMethodsInUser(){}


    @Pointcut("within(com.javaweb.dao..*)")
    public void daoLayerJoinPoints() {}

    @Pointcut("execution(* com.javaweb.service..*.*(..))")
    public void serviceLayer(){}

    @Pointcut("execution(* com.javaweb.dao.*.*(..))")
    public void specificedMethod() {}


    @Pointcut("within(com.javaweb.dao..*) && args(user)")
    public void withArgs(User user) {}

    @Pointcut("daoLayerJoinPoints() && args(String,String)")
    public void selectByRoleName(){}
//
//    @Pointcut("this(com.javaweb.dao..)")
//    public void thisUserDao() {}
    /**
     * define advice method, referenced to defined pointcut.
     */
//    @Around("daoLayerJoinPoints()")
    @Around("selectMethodsInUser()")
    public Object performanceLog(ProceedingJoinPoint pjp) {
        Long startTime = System.currentTimeMillis() ;
        Object ret = null ;
        try {
            ret = pjp.proceed();
            log.info(" method at: " + pjp.getSignature().getName()+ ", and executed spent: " +
                    (System.currentTimeMillis() - startTime) / 1000.0 + " s");
            return ret ;
        } catch (Throwable e) {
            log.error("error: " + e.getMessage()) ;
            return ret ;
        }

    }
//    @Before("withArgs(user)")
//    public void checkParamBeforeInvoke(JoinPoint jp, User user) {
//
//        log.info("method : " + jp.getSignature().getName() + ", "
//                        + jp.getSignature().getDeclaringTypeName()) ;
//
//    }
//    @After("thisUserDao()")
//    public void afterInvoke(JoinPoint jp) {
//        log.info("after method: " + jp.getSignature().getName()) ;
//    }




}
